const storage_constants = {
	searchHistory:'searchHistory',
	paramSongList:'paramSongList',
	mySongList:'mySongList',
	playList:'playList',
	playMode:'playMode',
	progress:'progress',
	playSeconds:'playSeconds',
	playDuration:'playDuration',
	playIndex:'playIndex',
	isFirstRun:'isFirstRun',
	recentlyPlayList:'recentlyPlayList'
}


function set(key, value) {
	try {
		uni.setStorageSync(key, value);
		return true;
	} catch (e) {
		return false;
	}
}

function get(key) {
	try {
		return uni.getStorageSync(key);
	} catch (e) {
		return null;
	}
}

function getDefault(key, defaultVal) {
	try {
		let val = uni.getStorageSync(key);
		if(val || val == 0){
			return val;
		}else{
			val = defaultVal;
			return val;
		}
		
	} catch (e) {
		return defaultVal;
	}
}

function getObj(key) {
	try {
		const val = uni.getStorageSync(key);
		if (val) {
			return val;
		}
		return null;
	} catch (e) {
		return null;
	}
}

function getObjDefault(key, defaultVal) {
	try {
		const val = uni.getStorageSync(key);
		if (val) {
			return val;
		}
		return defaultVal;
	} catch (e) {
		return defaultVal;
	}
}

function getArray(key) {
	try {
		const val = uni.getStorageSync(key);
		if (val) {
			return val;
		}
		return [];
	} catch (e) {
		return [];
	}
}

function getArrayDefault(key, defaultVal) {
	try {
		const val = uni.getStorageSync(key);
		if (val) {
			return val;
		}
		return defaultVal;
	} catch (e) {
		return defaultVal;
	}
}

function clearStorage(){
	uni.clearStorage();
	let item = {
		coverImg:"/static/mylike.png",
		name:`我喜欢的音乐`,
		id:"mylike",
		url:null,
		singer:"我喜欢的音乐",
		desc:null,
		platform:'local',
		album:null,
		list:[]
	}
	let allSongList = [];
	allSongList.push(item);
	set(storage_constants.mySongList,allSongList)
}

function remove(key){
	uni.removeStorageSync(key);
}


const storage = {
	set,
	get,
	getDefault,
	getObj,
	getObjDefault,
	getArray,
	getArrayDefault,
	clearStorage,
	remove
}

export {
	storage,
	storage_constants
}
